package com.akk.platform.oem.business;

import com.akk.module.avs.entity.AvsYbAppoCard;
import com.akk.module.avs.enums.AppoCardStatus;
import com.akk.module.avs.service.AvsYbAppoCardService;
import com.akk.utils.PageUtils;
import com.akk.utils.Query;
import com.akk.utils.RestResponse;
import com.akk.utils.ShiroUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class YbAppoCardBusiness {

    @Autowired
    private AvsYbAppoCardService appoCardService;

    public RestResponse list(Query query, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),null);
        PageHelper.startPage(query.getPage(),query.getLimit());
        Map params = query.getSearch();
        params.put("appId", ShiroUtils.getAdmin().getAppId());
        List<AvsYbAppoCard> list = appoCardService.findByParams(params);
        return RestResponse.build(PageUtils.buildPage(list,(int) new PageInfo<>(list).getTotal(),query));
    }

    public RestResponse save(AvsYbAppoCard appoCard,BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),null);
        appoCard.setCreateDate(new Date());
        appoCard.setStatus(AppoCardStatus.PENDING.getValue());
        appoCardService.save(appoCard);
        return RestResponse.build("200","success",null);
    }

    public RestResponse update(AvsYbAppoCard card){
        AvsYbAppoCard avsYbAppoCard = appoCardService.findById(card.getAppoId());
        if (avsYbAppoCard == null)
            return RestResponse.build("1","信息不存在",null);
        if (avsYbAppoCard.getStatus() == AppoCardStatus.AGREE.getValue() || avsYbAppoCard.getStatus() == AppoCardStatus.REFUSE.getValue())
            return RestResponse.build("1","此条信息已处理",null);
        avsYbAppoCard.setStatus(card.getStatus());
        appoCardService.update(avsYbAppoCard);
        return RestResponse.build("200","success",null);
    }

}
